<?php

namespace app\health\library;

use Exception;
use think\exception\Handle;

/**
 * 自定义API模块的错误显示
 */
class ExceptionHandle extends Handle
{

    public function render(Exception $e)
    {
        $statuscode = $code = 500;
        $msg = $e->getMessage() ?: 'An error occurred';
        // 验证异常
        if ($e instanceof \think\exception\ValidateException) {
            $code = 0;
            $statuscode = 200;
            $msg = $e->getError();
        }
        // Http异常
        if ($e instanceof \think\exception\HttpException) {
            $statuscode = $code = $e->getStatusCode();
        }
        return json(['code' => $code, 'msg' => $msg, 'time' => time(), 'data' => null], $statuscode);
    }
}
